﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASS.BUS.DTO;
using ASS.BUS.Services;

namespace ASS
{
    public partial class FrmEditInforPatient : Form
    {
        FrmMainClinicStaff _frmMain;
        private BenhNhan _benhNhan;
        public FrmEditInforPatient(BenhNhan benhNhan, FrmMainClinicStaff frmMain)
        {
            InitializeComponent();
            _benhNhan = benhNhan;
            _frmMain = frmMain;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEditInforPatient_Load(object sender, EventArgs e)
        {
            txtNamePatient.Text = _benhNhan.Ten;
            txtAddress.Text = _benhNhan.DiaChi;
            var yearStrings = new string[90];
            var beginYear = DateTime.Now.Year - 100;
            for (var i = 0; i < 90; ++i)
            {
                yearStrings[i] = beginYear.ToString();
                beginYear++;
            }
            cboBirthday.DataSource = yearStrings;
            cboBirthday.SelectedItem = _benhNhan.NgaySinh;

            string[] genderStrings = { "Nam", "Nữ" };
            cboGender.DataSource = genderStrings;
            cboGender.SelectedItem = _benhNhan.GioiTinh;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var benhNhanServices = new BenhNhanServices();
            var thanNhanServices = new ThanNhanServices();

            _benhNhan.Ten = txtNamePatient.Text;
            _benhNhan.DiaChi = txtAddress.Text;

            if (cboBirthday.SelectedIndex != -1)
            {
                _benhNhan.NgaySinh = cboBirthday.SelectedValue.ToString();
            }

            if (cboGender.SelectedIndex != -1)
            {
                _benhNhan.GioiTinh = cboGender.SelectedValue.ToString();
            }

            //ThanNhanBenhNhan thanNhan = thanNhanServices.GetThanNhan(_benhNhan.MaBn);

            //if (thanNhan != null)
            //{
            //    thanNhan.Ten = txtNameRelative.Text;
            //    thanNhan.SoDienThoai = txtPhoneNumber.Text;
            //}

            var editSuccess = benhNhanServices.EditBenhNhan(_benhNhan);
            //var editThanNhan = thanNhanServices.EditThanNhan(thanNhan);
            // bool addThanNhan = thanNhanServices.Add(thanNhan);
            MessageBox.Show((editSuccess /*&& addThanNhan*/) ? "Đã chỉnh sửa thông tin bệnh nhân!" : "Không sửa thông tin bệnh nhân!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            _frmMain.dvgPatient.DataSource = benhNhanServices.GetBenhNhans();
            this.Close();
        }
    }
}
